package concurrency.chapter6;

import javax.sound.midi.Soundbank;

public class ThreadInterrupt {
    private static final Object MONITOR = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread() {
            @Override
            public void run() {
                while (true) {
                    //System.out.println(">>" + this.isInterrupted());
                    /*try {
                        Thread.sleep(10);//sleep的当前线程t
                    } catch (InterruptedException e) {
                        System.out.println("收到打断信号");
                        e.printStackTrace();
                    }*/

                    synchronized (MONITOR) {
                        try {
                            //使用wait时必须给一个monitor，monitor要用synchronized包装
                            MONITOR.wait(10);//wait的当前线程t
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        t.start();
        Thread.sleep(100);
        System.out.println(t.isInterrupted());
        //wait、join、sleep时中断会抛异常InterruptedException
        t.interrupt();
        System.out.println(t.isInterrupted());
        //t.stop();//不要用这个stop打断

        //****************
        Thread t2 = new Thread(() -> {
            while (true) {
                //这个静态方法其实调用的是currentThread().isInterrupted(true);
                System.out.println(Thread.interrupted());
            }
        });

    }
}
